import axios from "axios";
import store from "@/store";
import router from "@/router/index";
import { notification } from "ant-design-vue";
import Cookies from 'js-cookie';
const setLoadingState = (e) => store.commit("setLoadingState", e);
import { apis } from "./apis";
//axios实例
const service = axios.create({
  baseURL: process.env.VUE_APP_API_URL,
  timeout: 100000,
});

//axios请求拦截器
service.interceptors.request.use(
  (config) => {
    setLoadingState(true);
    config.headers["Content-Language"] = "zh_CN";
    config.headers["AUTH-ID"] = localStorage.getItem("authid");
    config.headers["Authorization"] = Cookies.get('access');
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

//axios响应拦截器
service.interceptors.response.use(
  (res) => {
    setLoadingState(false);
    return Promise.resolve(res);
  },
  (error) => {
    setLoadingState(false);
    return Promise.reject(error.response);
  }
);

//封装请求函数
export const https = (url, params = {},method="GET") => {
  if (method == "get" || method == "GET") {
    return new Promise((resolve, reject) => {
      service
        .get(url, { params: params })
        .then((res) => {
          if (res && res.data) resolve(res);
        })
        .catch((err) => {
          reject(err);
        });
    });
  } else {
    return new Promise((resolve, reject) => {
      service
        .post(url, params)
        .then((res) => {
          if (res && res.data) resolve(res);
        })
        .catch((err) => {
          reject(err);
        });
    });
  }
};